﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace WPS.SDK.model.oauth
{
    public class Token
    {
        public string appid;
        public long expiresIn;
        public string accessToken;
        public string refreshToken;
        public string openid;

        public static Token fromJsonObject(JObject jsonObject)
        {
            Token token = new Token();
            JObject tokenObject = null;
            if (jsonObject["token"]!=null)
            {
                tokenObject = JsonConvert.DeserializeObject<JObject>(jsonObject["token"].ToString());
            }
            if (tokenObject == null)
            {
                return token;
            }
            token.appid = tokenObject["appid"]==null ? "" : tokenObject["appid"].ToString();
            token.accessToken = tokenObject["access_token"] == null ? "" : tokenObject["access_token"].ToString(); 
            token.refreshToken = tokenObject["refresh_token"] == null ? "" : tokenObject["refresh_token"].ToString();
            token.openid = tokenObject["openid"] == null ? "" : tokenObject["openid"].ToString();
            token.expiresIn = tokenObject["expires_in"] == null ? 0 : Convert.ToInt64(tokenObject["expires_in"].ToString());
            return token;
        }
    }
}
